//
//  MineOrderTwoTableViewCell.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/7.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "MineOrderTwoTableViewCell.h"

@interface MineOrderTwoTableViewCell()
@property (nonatomic, strong) UIImageView *titleImageView;
@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UIButton *stateBtn; // 状态

@property (nonatomic, strong) UILabel *serverTimeLabel; // 服务时间
@property (nonatomic, strong) UILabel *orderDistanceLabel; // 订单距离
@property (nonatomic, strong) UILabel *customerAddressLabel; // 客户地址
@property (nonatomic, strong) UILabel *customerTelephoneLabel; // 客户电话
@property (nonatomic, strong) UIButton *makeSureOrderBtn; // 待支付 已完成 完成服务（状态按钮）
@property (nonatomic, strong) UIButton *telephoneBtn; // 打电话
@property (nonatomic, strong) UIButton *talkAboutBtn; // 聊天
@property (nonatomic, strong) UILabel *moneyLabel; // 支付金额
@end

@implementation MineOrderTwoTableViewCell
// 我的服务
- (void)setMyServerModelList:(MineOrderModelList *)myServerModelList
{
    _myServerModelList = myServerModelList;
    self.titleLabel.text = myServerModelList.order_name;
    self.serverTimeLabel.text = [NSString stringWithFormat:@"服务时间:%@",myServerModelList.add_time];
    self.orderDistanceLabel.text = [NSString stringWithFormat:@"订单距离:%@km",myServerModelList.distance];
    self.customerAddressLabel.text = [NSString stringWithFormat:@"客户地址:%@",myServerModelList.worker_addr];
    self.customerTelephoneLabel.text = [NSString stringWithFormat:@"电话:%@",myServerModelList.worker_tel];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",myServerModelList.order_amount];
    
    self.makeSureOrderBtn.hidden = NO;
    self.moneyLabel.hidden = NO;
    if ([myServerModelList.order_status isEqualToString:@"2"]){
        [_stateBtn setTitle:@"进行中" forState:UIControlStateNormal];
        [_makeSureOrderBtn setTitle:@"完成服务" forState:UIControlStateNormal];
        self.moneyLabel.hidden = YES;
    }else if ([myServerModelList.order_status isEqualToString:@"3"]){
        [_stateBtn setTitle:@"待支付" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(mainColor);
        [_makeSureOrderBtn setTitle:@"待支付" forState:UIControlStateNormal];
    }else if ([myServerModelList.order_status isEqualToString:@"4"]){
        [_stateBtn setTitle:@"待评价" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(mainColor);
        [_makeSureOrderBtn setTitle:@"待评价" forState:UIControlStateNormal];
    }else if ([myServerModelList.order_status isEqualToString:@"5"]){
        [_stateBtn setTitle:@"已完成" forState:UIControlStateNormal];
        [_makeSureOrderBtn setTitle:@"已完成" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(mainColor);
    }
    
}
// 我的需求数据源
- (void)setMineOrderModelList:(MineOrderModelList *)mineOrderModelList
{
    _mineOrderModelList = mineOrderModelList;
    self.titleLabel.text = mineOrderModelList.order_name;
    self.serverTimeLabel.text = [NSString stringWithFormat:@"服务时间:%@",mineOrderModelList.add_time];
    self.orderDistanceLabel.text = [NSString stringWithFormat:@"订单距离:%@km",mineOrderModelList.distance];
    self.customerAddressLabel.text = [NSString stringWithFormat:@"客户地址:%@",mineOrderModelList.worker_addr];
    self.customerTelephoneLabel.text = [NSString stringWithFormat:@"电话:%@",mineOrderModelList.worker_tel];
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",mineOrderModelList.order_amount];
    
    self.makeSureOrderBtn.hidden = NO;
    self.moneyLabel.hidden = NO;
    if ([mineOrderModelList.order_status isEqualToString:@"1"]) {
        [_stateBtn setTitle:@"待确认" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(bgColor);
        [_makeSureOrderBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        self.moneyLabel.hidden = YES;
    }else if ([mineOrderModelList.order_status isEqualToString:@"2"]){
        [_stateBtn setTitle:@"进行中" forState:UIControlStateNormal];
        self.makeSureOrderBtn.hidden = YES;
        self.moneyLabel.hidden = YES;
    }else if ([mineOrderModelList.order_status isEqualToString:@"3"]){
        [_stateBtn setTitle:@"待支付" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(mainColor);
        [_makeSureOrderBtn setTitle:@"待支付" forState:UIControlStateNormal];
    }else if ([mineOrderModelList.order_status isEqualToString:@"4"]){
        [_stateBtn setTitle:@"待评价" forState:UIControlStateNormal];
        _makeSureOrderBtn.backgroundColor = getColor(@"FF7322");
        [_makeSureOrderBtn setTitle:@"去评价" forState:UIControlStateNormal];
    }else if ([mineOrderModelList.order_status isEqualToString:@"5"]){
        [_stateBtn setTitle:@"已完成" forState:UIControlStateNormal];
        self.makeSureOrderBtn.hidden = YES;
    }
    
}
- (void)btnAction:(UIButton *)btn
{
    if ([btn isEqual:self.makeSureOrderBtn]) {
        if (self.btnActionBlock) {
            self.btnActionBlock(self.mineOrderModelList.order_status);
        }
    }
    if ([btn isEqual:self.talkAboutBtn]) {
        NSLog(@"聊天");
        if (self.talkAboutBtnBlock) {
            self.talkAboutBtnBlock();
        }
    }
    if ([btn isEqual:self.telephoneBtn]) {
        NSLog(@"打电话");
        if (self.telephoneBtnBlock) {
            self.telephoneBtnBlock();
        }
    }
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsZero;
        
        [self.contentView addSubview:self.titleImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.stateBtn];
        UIView *lineView1 = [[UIView alloc]init];
        lineView1.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:lineView1];
        [self.contentView addSubview:self.serverTimeLabel];
        [self.contentView addSubview:self.orderDistanceLabel];
        [self.contentView addSubview:self.customerAddressLabel];
        UIView *lineView2 = [[UIView alloc]init];
        lineView2.backgroundColor = getColor(dividerColor);
        [self.contentView addSubview:lineView2];
        [self.contentView addSubview:self.customerTelephoneLabel];
        [self.contentView addSubview:self.makeSureOrderBtn];
        [self.contentView addSubview:self.telephoneBtn];
        [self.contentView addSubview:self.talkAboutBtn];
        [self.contentView addSubview:self.moneyLabel];
        
        __weak typeof(self)weakself = self;
        [self.titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.mas_top).offset(10 / WIDTH_6S_SCALE);
            make.left.equalTo(weakself.mas_left).offset(20 / WIDTH_6S_SCALE);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.titleImageView.mas_centerY);
            make.left.equalTo(weakself.titleImageView.mas_right).offset(5 / WIDTH_6S_SCALE);
            make.width.mas_equalTo(200 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        [self.stateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.titleImageView.mas_centerY);
            make.right.equalTo(weakself.mas_right).offset(-20 / WIDTH_6S_SCALE);
            make.width.mas_equalTo(60 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
        }];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.titleLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.right.left.equalTo(weakself);
            make.height.mas_equalTo(0.5);
        }];
        [self.serverTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView1.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(weakself.titleImageView);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(250 / WIDTH_6S_SCALE);
        }];
        [self.orderDistanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.serverTimeLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(weakself.titleImageView);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(200 / WIDTH_6S_SCALE);
        }];
        [self.customerAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.orderDistanceLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(weakself.titleImageView);
            //            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(230 / WIDTH_6S_SCALE);
        }];
        [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.customerAddressLabel.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.right.left.equalTo(weakself);
            make.height.mas_equalTo(0.5);
        }];
        [self.customerTelephoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lineView2.mas_bottom).offset(10 / HEIGHT_6S_SCALE);
            make.left.equalTo(weakself.titleImageView);
            make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
            make.width.mas_equalTo(200 / WIDTH_6S_SCALE);
        }];
        [self.makeSureOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.mas_centerY).offset(15 / HEIGHT_6S_SCALE);
            make.right.equalTo(weakself.stateBtn.mas_right);
            make.width.mas_equalTo(70 / WIDTH_6S_SCALE);
            make.height.mas_equalTo(30 / HEIGHT_6S_SCALE);
        }];
        self.makeSureOrderBtn.layer.cornerRadius = 5 / HEIGHT_6S_SCALE;
        self.makeSureOrderBtn.layer.masksToBounds = YES;
        [self.talkAboutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.customerTelephoneLabel.mas_centerY);
            make.right.equalTo(weakself.stateBtn.mas_right);
        }];
        [self.telephoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.customerTelephoneLabel.mas_centerY);
            make.right.equalTo(weakself.talkAboutBtn.mas_left).offset(-20 / WIDTH_6S_SCALE);
        }];
        [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakself.makeSureOrderBtn.mas_centerY).offset(-20 / HEIGHT_6S_SCALE);
            make.right.equalTo(weakself.makeSureOrderBtn.mas_left).offset(-30 / WIDTH_6S_SCALE);
        }];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakself.titleImageView.mas_top);
            make.left.right.equalTo(weakself);
            make.bottom.equalTo(weakself.customerTelephoneLabel.mas_bottom);
        }];
        
        
    }
    return self;
}
#pragma mark - init
- (UIImageView *)titleImageView
{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc]init];
        _titleImageView.backgroundColor = [UIColor clearColor];
        _titleImageView.image = [UIImage imageNamed:@"list_ico"];
    }
    return _titleImageView;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = getColor(blackColor);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = DEF_FontSize_14;
        [_titleLabel sizeToFit];
        _titleLabel.text = @"水电维修工订单";
    }
    return _titleLabel;
}
- (UIButton *)stateBtn
{
    if (!_stateBtn) {
        _stateBtn = [[UIButton alloc]init];
        _stateBtn.backgroundColor = [UIColor clearColor];
        [_stateBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        [_stateBtn setImage:[UIImage imageNamed:@"list_wjleftimg"] forState:UIControlStateNormal];
        [_stateBtn setTitle:@"待确认" forState:UIControlStateNormal];
        _stateBtn.titleLabel.font = DEF_FontSize_12;
        //        [_stateBtn addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
        [_stateBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -95 / WIDTH_6S_SCALE)];
        [_stateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -5 / WIDTH_6S_SCALE, 0, 0)];
    }
    return _stateBtn;
}

- (UILabel *)serverTimeLabel
{
    if (!_serverTimeLabel) {
        _serverTimeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _serverTimeLabel.backgroundColor = [UIColor clearColor];
        _serverTimeLabel.textColor = getColor(textColor);
        _serverTimeLabel.textAlignment = NSTextAlignmentLeft;
        _serverTimeLabel.numberOfLines = 0;
        _serverTimeLabel.font = DEF_FontSize_14;
        [_serverTimeLabel sizeToFit];
        _serverTimeLabel.text = @"服务时间:2018-01-06 上午";
    }
    return _serverTimeLabel;
}
- (UILabel *)orderDistanceLabel{
    if (!_orderDistanceLabel) {
        _orderDistanceLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _orderDistanceLabel.backgroundColor = [UIColor clearColor];
        _orderDistanceLabel.textColor = getColor(textColor);
        _orderDistanceLabel.textAlignment = NSTextAlignmentLeft;
        _orderDistanceLabel.numberOfLines = 0;
        _orderDistanceLabel.font = DEF_FontSize_14;
        [_orderDistanceLabel sizeToFit];
        _orderDistanceLabel.text = @"订单距离:全程500米";
    }
    return _orderDistanceLabel;
}
- (UILabel *)customerAddressLabel
{
    if (!_customerAddressLabel) {
        _customerAddressLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _customerAddressLabel.backgroundColor = [UIColor clearColor];
        _customerAddressLabel.textColor = getColor(textColor);
        _customerAddressLabel.textAlignment = NSTextAlignmentLeft;
        _customerAddressLabel.numberOfLines = 0;
        _customerAddressLabel.font = DEF_FontSize_14;
        [_customerAddressLabel sizeToFit];
        _customerAddressLabel.text = @"客户地址:铁西广场库尔大厦";
    }
    return _customerAddressLabel;
}
- (UILabel *)customerTelephoneLabel
{
    if (!_customerTelephoneLabel) {
        _customerTelephoneLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _customerTelephoneLabel.backgroundColor = [UIColor clearColor];
        _customerTelephoneLabel.textColor = getColor(textColor);
        _customerTelephoneLabel.textAlignment = NSTextAlignmentLeft;
        _customerTelephoneLabel.numberOfLines = 0;
        _customerTelephoneLabel.font = DEF_FontSize_14;
        [_customerTelephoneLabel sizeToFit];
        _customerTelephoneLabel.text = @"电话:13998762559";
    }
    return _customerTelephoneLabel;
}
- (UIButton *)makeSureOrderBtn
{
    if (!_makeSureOrderBtn) {
        _makeSureOrderBtn = [[UIButton alloc]init];
        _makeSureOrderBtn.backgroundColor = getColor(mainColor);
        [_makeSureOrderBtn setTitleColor:getColor(whiteColor) forState:UIControlStateNormal];
        [_makeSureOrderBtn setTitle:@"待支付" forState:UIControlStateNormal];
        _makeSureOrderBtn.titleLabel.font = DEF_FontSize_12;
        [_makeSureOrderBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _makeSureOrderBtn;
}
- (UIButton *)telephoneBtn
{
    if (!_telephoneBtn) {
        _telephoneBtn = [[UIButton alloc]init];
        _telephoneBtn.backgroundColor = [UIColor clearColor];
        [_telephoneBtn setImage:[UIImage imageNamed:@"list_call"] forState:UIControlStateNormal];
        [_telephoneBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _telephoneBtn;
}
- (UIButton *)talkAboutBtn
{
    if (!_talkAboutBtn) {
        _talkAboutBtn = [[UIButton alloc]init];
        _talkAboutBtn.backgroundColor = [UIColor clearColor];
        [_talkAboutBtn setImage:[UIImage imageNamed:@"list_im"] forState:UIControlStateNormal];
        [_talkAboutBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _talkAboutBtn;
}
- (UILabel *)moneyLabel
{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _moneyLabel.backgroundColor = [UIColor clearColor];
        _moneyLabel.textColor = getColor(textColor);
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel.font = DEF_FontSize_14;
        [_moneyLabel sizeToFit];
        _moneyLabel.text = @"￥270";
    }
    return _moneyLabel;
}
@end

